// const fs = require('fs');
// const path = require('path');
// const inquirer = require('inquirer');
import fs from 'fs';
import path from 'path';
// import inquirer from 'inquirer';

const HOSTS_PATH = process.platform === 'win32'
    ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
    : '/etc/hosts';
// 读取配置项
const dataPath = path.resolve('data', 'data.json'); //'../data/data.json'
console.log('dataPath', dataPath);
const userOption = getOption();
/**
 * 获取配置
 * @returns 
 */
function getOption() {
    return JSON.parse(fs.readFileSync(dataPath, 'utf8'));
}
/**
 * 读取host
 * @returns 
 */
function readHostsFile() {
    try {
        // console.log('readHostsFile', { userOption });
        myLog(userOption)
        return fs.readFileSync(HOSTS_PATH, 'utf8');
    } catch (err) {
        console.error('Failed to read hosts file:', err);
        return null;
    }
}
/**
 * 写入host
 * @param {*} content 
 * @returns 
 */
function writeHostsFile(content) {
    try {
        fs.writeFileSync(HOSTS_PATH, content, 'utf8');
        return true;
    } catch (err) {
        console.error('Failed to write hosts file:', err);
        return false;
    }
}
/**
 * 修改host 配置
 * @param {*} res           Array[Object]       {ip,hostname}
 * @param {*} ip 
 * @param {*} hostname 
 * @returns 
 */
function modifyHostsFile(res, text) {
    const hostsContent = readHostsFile();
    if (!hostsContent) return false;

    let newEntry = text || '';
    res.forMap(rest => {
        const { ip, hostname } = rest;
        newEntry += `${ip} ${hostname}\r\n`;
    })

    const updatedContent = hostsContent.replace(newEntry, '') + newEntry;

    return writeHostsFile(updatedContent);
}
/**
 * 获取hots地址
 * @returns 
 */
function getFilePath() {
    return HOSTS_PATH
}
/**
 * 交互式打印封装
 * @param {*} content 
 * @param {*} questions 
 */
function myLog(content, questions = {
    type: 'confirm',
    name: 'showDetails',
    message: '是否查看详细内容？',
    default: false
}) {
    // inquirer.prompt(questions).then(answers => {
    //     if (answers.showDetails) {
    //         console.log(content);
    //     } else {
    //         console.log('Header: [点击展开]');
    //     }
    // });
}

function start() {
    const spStartText = `# nodeHost-start----------------\r\n`
    const spEndText = `# nodeHost-end----------------\r\n`
    // 查询本地配置文件
    const hostContent = readHostsFile();
    // 读取host配置
    let userOption = getOption();
    // 切割数据取出添加部分
    let hostList = []
    hostContent.split(spStartText)[1].split(spEndText)[0].split(`\r\n`).forEach(rest => {
        if (rest) {
            hostList.push(rest)
        }
    })
    // 处理
    // console.log('start-->', { userOption, hostContent, hostList })
    // return
    // 对比存储数据更新host
    if (userOption.endData.content != hostContent) {
        // 缓存记录
        userOption.startData.push({
            content: hostContent,
            time: new Date().getTime()
        })
        // 更新host
        // userOption.endData = {
        //     content: hostContent + 
        // }

    }
    // 启动代理服务
}

export default {
    start,
    userOption,
    readHostsFile,
    writeHostsFile,
    modifyHostsFile,
    getFilePath,
    getOption
}